import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from matplotlib import pyplot
from sklearn.preprocessing import StandardScaler

# lsi_matrix = np.loadtxt(open("../model/all_lsi.csv","rb"),delimiter=",",skiprows=0)
lsi_matrix = np.loadtxt(open("../model/keyword_word2vec.csv","rb"),delimiter=",",skiprows=0)
# lsi_matrix = np.load('../model/doc_vectors_2row.npy')
df_train = pd.read_csv('../data/train_processed.csv')

X = lsi_matrix[0:4774, :]
Y = df_train['label'].values

# 数据标准化
# ss_X = StandardScaler()
# X = ss_X.fit_transform(X)
def fit_grid_point_RBF(C, gamma, X_train, y_train, X_val, y_val, pre_result):
    # 在训练集是那个利用SVC训练
    svc_model = SVC(C=C, kernel='rbf', gamma=gamma, probability=1)
    svc_model = svc_model.fit(X_train, y_train)
    prdict = svc_model.predict_proba(X_val)
    prdict_result = pre_result + prdict
    # 在校验集上返回accuracy
    accuracy = svc_model.score(X_val, y_val)

    # print("accuracy: %f, C：%f，gamma：%s" % (accuracy, C, gamma))
    return accuracy, prdict_result
# C_s = np.logspace(-1, 1, 3)# logspace(a,b,N)把10的a次方到10的b次方区间分成N份
# gamma_s = np.logspace(-2, 2, 5)


# X_train_1, X_val_1, y_train_1, y_val_1 = train_test_split(X, Y, test_size=0.10, random_state=j)
# prdict_result = np.zeros((len(y_val_1), 11), dtype=np.float64)

X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.10, random_state=3)
# n_estimators= [250,300,500]
# param_test1 = dict(n_estimators=n_estimators)
# param_test2= {'max_depth':range(9,14,2), 'min_samples_split':range(2,10,2)}
# param_test3= {'min_samples_split':range(2,10,2), 'min_samples_leaf':range(2,10,2)}
# param_test4= {'max_features':range(3,11,2)}
# gsearch1= GridSearchCV(estimator = RandomForestClassifier(n_estimators= 200,min_samples_split=2,
#                                  min_samples_leaf=2,max_depth=13,max_features=7,random_state=10),
#                        param_grid =param_test1, scoring='accuracy',cv=5)
# gsearch1.fit(X_train,y_train)
# print(gsearch1.grid_scores_)
# print(gsearch1.best_params_)
# print(gsearch1.best_score_)

for i in range(1):
    seed = i
    np.random.seed(seed)
    X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.10, random_state=seed)

    rf = RandomForestClassifier(n_estimators=1000, max_depth=13, min_samples_split=2,
                                 min_samples_leaf=2, max_features=7, oob_score=True, random_state=10,n_jobs=-1)
    rf.fit(X_train, y_train)
    accuracy = rf.score(X_val, y_val)
    print(accuracy)

# final_predict = np.argmax(prdict_result, axis=1) + 1
# print ("Accuracy of val: %f"%accuracy_score(y_val_1, final_predict))

# accuracy_s1 =np.array(accuracy_s).reshape(len(C_s),len(gamma_s))
# x_axis = np.log10(C_s)
# for j, gamma in enumerate(gamma_s):
#     pyplot.plot(x_axis, np.array(accuracy_s1[:,j]), label = ' Test - log(gamma)' + str(gamma))
#
# pyplot.legend()
# pyplot.xlabel( 'log(C)' )
# pyplot.ylabel( 'accuracy' )
# pyplot.savefig('RBF_SVM.png' )

# pyplot.show()
# 最佳参数是C:12,gamma:0.007